package org.jeecg.modules.purchase.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.api.model.SysDepartVO;
import org.jeecg.common.system.api.model.SysMessageTemplate;
import org.jeecg.common.util.TimingUtils;
import org.jeecg.modules.purchase.entity.MerchantProvider;
import org.jeecg.modules.purchase.service.IMerchantProviderService;
import org.jeecg.common.system.api.model.XxlSendDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/purchase/timing")
@Api(tags="返回过期消息")
public class TimingController {
    
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private IMerchantProviderService merchantProviderService;
    
    private static final String ENABLE = "Y";
    private static final String NDEL_FLAG = "0";
    
    @GetMapping(value = "/getProviderExpried")
    @ApiOperation(value="返回供应商过期消息", notes="返回供应商过期消息")
    public Result getContentExpried(String messageTemplateCode, int noticeDay){
        Map<String, Object> map = new HashMap<>();
        
        //获取消息模板数据
        //SysMessageTemplate sysMessageTemplate = sysBaseAPI.getMsgTemplateByCode(messageTemplateCode).getResult();
        Result<SysMessageTemplate> msgTemplateByCode = sysBaseAPI.getMsgTemplateByCode(messageTemplateCode);
        SysMessageTemplate sysMessageTemplate = msgTemplateByCode.getResult();
        List<XxlSendDto> list = new ArrayList<>();
        Map<String,String> param = new HashMap<>();
        XxlSendDto xxlSendDto;
        
        //获取所有处于为删除、启用状态，失效时间小于过期提醒时间，大于当前时间的所有数据
        String expiredDay = DateUtil.format(TimingUtils.getExpiredDay(new Date(), noticeDay), "yyyy-MM-dd");
        List<MerchantProvider> merchantProviders = merchantProviderService.getExpried(expiredDay);
        
        //循环存值
        for(MerchantProvider merchantProvider : merchantProviders){
            //获取参数内容
            String name = merchantProvider.getName() ;
            String date = DateUtil.format(merchantProvider.getEndTime(), "yyyy-MM-dd");
            param.clear();
            //存参数，模板参数
            param.put("content", name);
            param.put("date", date);
            //获得发送消息标题和内容
            String title = TimingUtils.getTitle(sysMessageTemplate.getTemplateName(), param);
            String content = TimingUtils.getTitle(sysMessageTemplate.getTemplateContent(), param);
            String id = merchantProvider.getId();
            //存这条过期记录的id
            param.put("id", id);
            xxlSendDto = new XxlSendDto(param, title, content, id);
            list.add(xxlSendDto);
        }
        
        map.put("msgs", list);
        
        return Result.OK(map);
    }
    
}
